package com.fly.controller;

import com.fly.bean.Fix;
import com.fly.bean.SystemPropertiesValue;
import com.fly.bean.Users;
import com.fly.service.FixTaskService;
import com.fly.util.JWTUtil;
import com.fly.util.Msg;
import com.fly.vo.TaskPageFind_WH;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class FixTaskController {

    @Resource
    private FixTaskService fixTaskService;

    //分页查询所有的消缺任务
    @RequestMapping("/selectFixTask")
    @ResponseBody
    public Msg selectFixTask(TaskPageFind_WH taskPageFind_wh, HttpServletRequest request) {

        String token = request.getHeader("Authorization");

        Users users = JWTUtil.unsign(token, Users.class);

        if (users != null) {
            taskPageFind_wh.setUserId(users.getUserId());
            if(fixTaskService.selectUserRoleByUserId(users.getUserId()) == 1){
                taskPageFind_wh.setRoleId(null);
            }else {
                taskPageFind_wh.setRoleId(fixTaskService.selectUserRoleByUserId(users.getUserId()));
            }
        }


        int count = fixTaskService.selectFixCountByPage(taskPageFind_wh);

        List<Fix> fix = fixTaskService.selectFixByPage(taskPageFind_wh);

        return Msg.success().add("count", count).add("fix", fix);
    }

    //查询所有的消缺任务状态
    @RequestMapping("/selectAllFixState")
    @ResponseBody
    public Msg selectAllFixState() {

        List<SystemPropertiesValue> list = fixTaskService.selectAllFixState();
        return Msg.success().add("fixState", list);
    }

    //条件查询消缺任务并且分页
    @RequestMapping("/selectFixTaskByFind")
    @ResponseBody
    public Msg selectFixTaskByFind(TaskPageFind_WH taskPageFind_wh, HttpServletRequest request) {

        String token = request.getHeader("Authorization");

        Users users = JWTUtil.unsign(token, Users.class);

        if (users != null) {
            taskPageFind_wh.setUserId(users.getUserId());
            if(fixTaskService.selectUserRoleByUserId(users.getUserId()) == 1){
                taskPageFind_wh.setRoleId(null);
            }else {
                taskPageFind_wh.setRoleId(fixTaskService.selectUserRoleByUserId(users.getUserId()));
            }
        }

//        传过来的参没有值就设置为空,有就在参数前后添加%%模糊
        if (taskPageFind_wh.getTaskName() == "") {
            taskPageFind_wh.setTaskName(null);
        } else {
            taskPageFind_wh.setTaskName("%" + taskPageFind_wh.getTaskName() + "%");
        }

        if (taskPageFind_wh.getTaskNo() == "") {
            taskPageFind_wh.setTaskNo(null);
        } else {
            taskPageFind_wh.setTaskNo("%" + taskPageFind_wh.getTaskNo() + "%");
        }

        if (taskPageFind_wh.getUserName() == "") {
            taskPageFind_wh.setUserName(null);
        } else {
            taskPageFind_wh.setUserName("%" + taskPageFind_wh.getUserName() + "%");
        }

        if (taskPageFind_wh.getStartDate() == "") {
            taskPageFind_wh.setStartDate(null);
        }

        if (taskPageFind_wh.getEndDate() == "") {
            taskPageFind_wh.setEndDate(null);
        }

        //默认为任务配置参数

        taskPageFind_wh.setSysProId(4);

        int count = fixTaskService.selectFixCountByFind(taskPageFind_wh);

        List<Fix> fix = fixTaskService.selectFixByFind(taskPageFind_wh);

        return Msg.success().add("count", count).add("fix", fix);
    }

    //指定Id查询消缺任务的详细信息显示
    @RequestMapping("/selectFixTaskByFixId")
    @ResponseBody
    public Msg selectFixTaskByFixId(int fixId) {

        Fix fix = fixTaskService.selectFixByFixId(fixId);

        List<Users> users1 = fixTaskService.selectUserByTaskId(fix.getTask().getTaskId());

        return Msg.success().add("fix", fix).add("fixUser", users1);
    }

}
